package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;
import jdk.nashorn.internal.scripts.JD;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserImpl implements UserDao {
    @Override
    public int add(UserModel userModel) {
        String sql = "insert into user(username,password) " +
                "values('" + userModel.getUsername() + "','" + userModel.getPassword() + "')";
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public UserModel findByUsername(String username) {
        String sql = "select * from user where username='" + username + "'";
        ResultSet resultSet = JDBC.getResultSet(sql);
        UserModel userModel = null;
        try {
            while (resultSet.next()) {
                userModel = new UserModel();
                userModel.setUsername(resultSet.getString("username"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }
        return userModel;
    }

    @Override
    public UserModel findByUsernameAndPassword(UserModel userModel) {
        String sql = "select * from user where username='"
                + userModel.getUsername() + "' and password='"
                + userModel.getPassword() + "'";
        ResultSet resultSet = JDBC.getResultSet(sql);
        UserModel model = null;
        try {
            while (resultSet.next()) {
                model = new UserModel();
                model.setUsername(resultSet.getString("username"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }
        return model;
    }

    @Override
    public List<UserModel> findAll() {

        String sql = "select * from user";
        ResultSet resultSet = JDBC.getResultSet(sql);
        List<UserModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {

                UserModel model = new UserModel();
                model.setUsername(resultSet.getString("username"));
                model.setPassword(resultSet.getString("password"));
                model.setId(resultSet.getInt("id"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }
        return list;
    }

    @Override
    public UserModel findById(int id) {
        String sql = "select * from user where id=" + id;
        ResultSet resultSet = JDBC.getResultSet(sql);
        UserModel model = null;
        try {
            while (resultSet.next()) {
                model = new UserModel();
                model.setUsername(resultSet.getString("username"));
                model.setPassword(resultSet.getString("password"));
                model.setId(resultSet.getInt("id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }
        return model;
    }

    @Override
    public int update(UserModel userModel) {
        String sql = "update user set " +
                "username='" + userModel.getUsername() + "', " +
                "password='" + userModel.getPassword() + "' " +
                "where id=" + userModel.getId();
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public int del(int i) {
        String sql = "delete from user where id=" + i;
        return JDBC.excuteUpdate(sql);
    }


}
